package org.jeecg.modules.system.upgrade.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.system.upgrade.dto.EditionDto;
import org.jeecg.modules.system.upgrade.dto.InsertEditionDto;
import org.jeecg.modules.system.upgrade.dto.VersionDto;
import org.jeecg.modules.system.upgrade.entity.BikeUpgradeAppVersion;
import org.jeecg.modules.system.upgrade.service.IBikeUpgradeAppVersionService;

import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: APP版本表
 * @Author: 月月鸟
 * @Date:   2021-07-14
 * @Version: V1.0
 */
@Api(tags="APP版本表")
@RestController
@RequestMapping("/upgrade/bikeUpgradeAppVersion")
@Slf4j
public class BikeUpgradeAppVersionController extends JeecgController<BikeUpgradeAppVersion, IBikeUpgradeAppVersionService> {
	@Autowired
	private IBikeUpgradeAppVersionService bikeUpgradeAppVersionService;
	 @Value("${application.domain}")
	 private String domain;


	 @ApiOperation(value="APP版本表-分页列表查询", notes="APP版本表-分页列表查询")
	 @GetMapping(value = "/getEditionList")
	 public Result<?> getEditionList(@RequestParam(value = "id",required = false) String id,@RequestParam(value = "newest",required = false) String newest,
									 @RequestParam(value = "appId",required = false)String appId,
									 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		 return Result.OK(bikeUpgradeAppVersionService.getEditionList(id,newest,appId,pageNo,pageSize));
	 }

	 @ApiOperation(value="APP版本表-查询下载全路径", notes="APP版本表-查询下载全路径")
	 @GetMapping(value = "/getProgram")
	 public Result<?> getProgram(@RequestParam(value = "appId",required = false)String appId) {
		String newest = "1";
		 VersionDto versionDto=bikeUpgradeAppVersionService.getProgram(newest,appId);
		 if (versionDto!=null){
			 return Result.OK(domain+versionDto.getDownloadUrl());
		 }else {
		 	return Result.OK();
		 }
	 }

	 @AutoLog(value = "新增版本")
	 @ApiOperation(value="新增版本", notes="新增版本")
	 @PostMapping(value = "/insertEdition")
	 public Result<?> insertEdition(@RequestBody InsertEditionDto insertEditionDto) {
		 return bikeUpgradeAppVersionService.insertEdition(insertEditionDto);
	 }

	 @AutoLog(value = "修改版本")
	 @ApiOperation(value="修改版本", notes="修改版本")
	 @PostMapping(value = "/updateEdition")
	 public Result<?> updateEdition(@RequestBody InsertEditionDto insertEditionDto) {
		 return bikeUpgradeAppVersionService.updateEdition(insertEditionDto);
	 }


	 @ApiOperation(value="查看下载记录", notes="查看下载记录")
	 @GetMapping(value = "/downRecord")
	 public Result<?> downRecord(@RequestParam(value = "versionId",required = false) String versionId,@RequestParam("pageNo") Integer pageNo,@RequestParam("pageSize")Integer pageSize) {
		 return bikeUpgradeAppVersionService.downRecord(versionId,pageNo,pageSize);
	 }


	 /**
	 *   添加
	 *
	 * @param bikeUpgradeAppVersion
	 * @return
	 */
	@AutoLog(value = "APP版本表-添加")
	@ApiOperation(value="APP版本表-添加", notes="APP版本表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BikeUpgradeAppVersion bikeUpgradeAppVersion) {
		bikeUpgradeAppVersionService.save(bikeUpgradeAppVersion);
		return Result.OK("添加成功！");
	}

	 /**
	  *   删除
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "APP版本表-删除")
	 @ApiOperation(value="APP版本表-删除", notes="APP版本表-删除")
	 @DeleteMapping(value = "/deleteEdition")
	 public Result<?> deleteEdition(@RequestParam("versionId")  String versionId) {
		 return bikeUpgradeAppVersionService.deleteEdition(versionId);
	 }

	 /**
	  *   设置为当前版本
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "设置为当前版本")
	 @ApiOperation(value="设置为当前版本", notes="设置为当前版本")
	 @PostMapping(value = "/updateApp")
	 public Result<?> updateApp(@RequestBody EditionDto editionDto) {
		 return bikeUpgradeAppVersionService.updateApp(editionDto);
	 }


	/**
	 *  编辑
	 *
	 * @param bikeUpgradeAppVersion
	 * @return
	 */
	@AutoLog(value = "APP版本表-编辑",operateType = 3)
	@ApiOperation(value="APP版本表-编辑", notes="APP版本表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BikeUpgradeAppVersion bikeUpgradeAppVersion) {
		bikeUpgradeAppVersionService.updateById(bikeUpgradeAppVersion);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "APP版本表-通过id删除")
	@ApiOperation(value="APP版本表-通过id删除", notes="APP版本表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		bikeUpgradeAppVersionService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "APP版本表-批量删除")
	@ApiOperation(value="APP版本表-批量删除", notes="APP版本表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.bikeUpgradeAppVersionService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="APP版本表-通过id查询", notes="APP版本表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BikeUpgradeAppVersion bikeUpgradeAppVersion = bikeUpgradeAppVersionService.getById(id);
		if(bikeUpgradeAppVersion==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(bikeUpgradeAppVersion);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param bikeUpgradeAppVersion
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BikeUpgradeAppVersion bikeUpgradeAppVersion) {
        return super.exportXls(request, bikeUpgradeAppVersion, BikeUpgradeAppVersion.class, "APP版本表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BikeUpgradeAppVersion.class);
    }

}
